package com.ek.config;

import java.util.Properties;

import org.eclipse.jetty.server.handler.ContextHandler;

import com.ek.controller.NewsController;
import com.ek.model._MappingKit;
import com.ek.routes.AdminRoutes;
import com.ek.routes.FrontRoutes;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

public class BaseConfig extends JFinalConfig {

	/**
	 * 开发模式配置
	 */
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		PropKit.use("jdbc.properties");
		me.setDevMode(true);
		me.setViewType(ViewType.JSP);
	}
	
	/**
	 * 创建数据库连接池
	 * @return
	 */
	public static C3p0Plugin  createC3p0Plugin() {
		// TODO Auto-generated method stub
		String url = PropKit.get("jdbc.url");
		String user = PropKit.get("jdbc.user");
		String password =  PropKit.get("jdbc.password");
		return new C3p0Plugin(url,user,password);
	}
	
	/**
	 * 路由器配置
	 */
	@Override
		public void configRoute(Routes me) {
			// TODO Auto-generated method stub
			me.add(new FrontRoutes());//前端路由
			me.add(new AdminRoutes());//后台路由
			
		}

	/**
	 * 配置插件
	 */
	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		//数据库连接池配置
		C3p0Plugin c3p0Plugin = createC3p0Plugin();
		me.add(c3p0Plugin);
		//配置ActiveRecord配置
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		//忽略大小写
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		me.add(arp);
		_MappingKit.mapping(arp);
	}
	
	/**
	 * 拦截器配置
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
	}
	//全部变量配置
	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		me.add(new ContextPathHandler("basePath"));
	}
}
